package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-08-08 10:16
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {


    @Autowired
    private CartInfoService cartInfoService;

    /**
     * 将商品加入到购物车
     *
     * @param skuId  商品ID
     * @param skuNum 商品数量
     * @return
     */
    @ApiOperation("将商品加入到购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("skuNum") int skuNum) {
        String userId = getCartUserId(request);
        cartInfoService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }

    /**
     * 查询用户（临时，登录）购物车列表
     *
     * @param request
     * @return
     */
    @ApiOperation("查询用户（临时，登录）购物车列表")
    @GetMapping("/cartList")
    public Result getCartList(HttpServletRequest request) {
        //1.先获取登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.先获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //3.调用业务层返回购物车商品列表
        List<CartInfo> list = cartInfoService.getCartList(userId, userTempId);
        return Result.ok(list);
    }


    /**
     * 修改购物车商品选中状态
     *
     * @param skuId
     * @param isChecked
     * @return
     */
    @ApiOperation("修改购物车商品选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("isChecked") int isChecked) {
        //1.先获取用户ID
        String userId = getCartUserId(request);

        //2.修改商品选中状态
        cartInfoService.checkCart(userId, skuId, isChecked);
        return Result.ok();
    }

    /**
     * 删除用户购物车中商品
     *
     * @param request
     * @param skuId
     * @return
     */
    @ApiOperation("删除用户购物车中商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        //1.获取用户ID
        String userId = getCartUserId(request);
        //2.调用业务层删除购物车商品
        cartInfoService.deleteCart(userId, skuId);
        return Result.ok();
    }

    /**
     * 获取购物车模块中需要用户ID
     *
     * @param request
     * @return
     */
    private String getCartUserId(HttpServletRequest request) {
        //1.优先获取当前登录真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.如果登录用户ID为空再获取临时用户ID
        if (StringUtils.isBlank(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        return userId;
    }


}
